package com.ihg.hiex.admin.service.account.impl;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.annotation.Dao;
import com.ihg.hiex.admin.dao.account.AccountDao;
import com.ihg.hiex.admin.service.DataServiceImpl;
import com.ihg.hiex.admin.service.account.AssessorService;
import com.ihg.hiex.entity.user.Account;
import com.ihg.hiex.entity.user.Assessor;

/**
 * 
 * @author ZEAL
 *        
 */
@Service("assessorService")
public class AssessorServiceImpl extends DataServiceImpl<Assessor> implements AssessorService {
    
    @Dao
    private AccountDao accountDao;
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AssessorService#create(com.ihg.hiex.entity.user.Assessor)
     */
    public void create(Assessor assessor) {
        
        // 1. load account by email
        Account account = accountDao.loadByEmail(assessor.getEmail().toLowerCase());
        
        if (account == null) {      // 2.1 if account no exit
            
            // 2.1.1 save a new account
            account = new Account(assessor);
            genericDaoSupport.save(account.initialize());
            
            // 2.1.2 save a new assessor
            assessor.initialize(account);
            genericDaoSupport.save(assessor);
           
        } else {                    // 2.2. if account exit
            
            // 2.2.1 if account is assessor, create fail
            if (account.hasAuthority("ASSESSOR")) {
                throw BusinessException.newInstance("500", "创建失败，该用户已存在Assessor角色");
            }
            
            // 2.2.2 save assessor & update account authority
            genericDaoSupport.save(assessor.initialize(account));
            genericDaoSupport.update(account.addAuthority("ASSESSOR"));
            
        }
        
    }

    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AssessorService#edit(com.ihg.hiex.entity.user.Assessor)
     */
    public void edit(Assessor assessor) {
        
        // 1. update account
        Account account = assessor.getAccount();
        account.edit(assessor.getTelephone());
        genericDaoSupport.update(account);
        
        // 2. update assessor
        genericDaoSupport.update(assessor.edit());
        
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AssessorService#disable(com.ihg.hiex.entity.user.Assessor)
     */
    public void disable(Assessor assessor) {
        
        // 1. get account by assessor
        Account account = assessor.getAccount();
        
        if (account.getAuthority().contains("ADMIN")) {       // 2.1 if account is administrator, then remove authority
            genericDaoSupport.update(account.removeAuthority("ASSESSOR"));
        } else {                                                // 2.2 if account is not administrator, then disable account
            genericDaoSupport.update(account.disable());
        }
        
        // 3. disable assessor
        genericDaoSupport.update(assessor.disable());
        
    }
    
}